<!--
PAGE DOCUMENTATION:
==================
This is a library containig all general use functions for the application.
See inline documentation for specific functions.
-->

<?php

	/*
	* @ params: 	$name - form name.
	*				$dest - the action field value (example: send.php)
	*				$nameList - a list of the input names.
	*				$valueList - a list of the input values.
	* @ purpose:	function creates and submits a form (the only way to redirect from a dialog box).
	* @ return:		void.
	*/	
	function redirectFromDialogBox($name,$dest,$nameList,$valueList) 
	{
		echo "<form id=\"$name\" action=\"$dest\" method=\"post\">";
		$nameArr = explode(" ",$nameList);
		$valueArr = explode(" ",$valueList);
		for ($i=0;$i<count($nameArr);$i++) {
			echo "<input type=\"hidden\" name=\"".$nameArr[$i]."\" value=".$valueArr[$i]." >";
		}
		echo "</form>
				<script>
					document.getElementById('$name').submit();
				</script>
			";
	}

	/*
	* @ params: 	$sqlDb - my sql DB object.
	*				$user_id - the id of the present user..
	*				$currentPage - the page on which the time settings are presented.
	* @ purpose:	function creates the time settings section.
	* @ return:		1 - for success, 0 - for failure.
	*/	
	function showTimeSettings($sqlDb,$user_id,$currentPage)
	{		
		if (($userOffSet = getColumnData($sqlDb,'sessions',$user_id,"time_offset")) != -13) {
			echo "<div id=\"timeDiv\" style=\"color:#3b5998; text-align:center\"><b>Time is presented in GMT </b>";
			if ($userOffSet) {
				if ($userOffSet < 0) {
					echo "<b>".$userOffSet."</b>";
				} else {
					echo "+ <b>".$userOffSet."</b>";
				}
			}
			echo "</br>";
			// create a time zone dialog:
			$dialogId = "set_time_offset";
			openTimeSettingsDialog($dialogId,$currentPage);
			echo "<a href=\"#\" clicktoshowdialog=\"$dialogId\"><font size=\"2\">Click here to change time settings</font></a>
				</div>";
			return 1;
			//</td></tr>
		} else {
			echo "<div id=\"timeDiv\" style=\"color:#3b5998; text-align:center\"><b>We cannot show your time settings at the moment.</b>";
			return 0;
		}
	}
		


	/*
	* @ params: 	$dialogId - string - a string representing the dialog's id, in which it will be called by facebook
	*							when a the user clicks the text that should open it. example "delete_dialog".
	*				$dialogTitle - the title of the dialog box.
	*				$dialogContent - string - all the text and form inside the dialog box.
	*				$formId - string - the id of the form in the dialog box, under which it will be submitted.
	*							example: "delete_form".
	*				$aprovePage - string - the page to direct the user to in case he chooses to approve the action.
	*							example: "send".
	*				$possitive - string - the text that will appear on the approve action button.
	* @ purpose:	function creates and echos the string that creates the requested dialog.
	* @ NOTES:		after this function there should be a line (or more) like this:
	*				"<a href=\"#\" clicktoshowdialog=\"$dialogId\"><font size=\"2\">delete</font></a>"
	*				to call the dialog box.
	* @ return:		void.
	*/	
	function createFaceBookDialog($dialogId,$dialogTitle,$dialogContent,$formId,$aprovePage,$possitive)
	{
		echo "<fb:dialog id=\"$dialogId\" cancel_button=1>
  				<fb:dialog-title>$dialogTitle</fb:dialog-title>	
  				<fb:dialog-content>$dialogContent</fb:dialog-content>";
  		if ($aprovePage==NULL) {
  			echo "<fb:dialog-button type=\"button\" value=\"$possitive\" form_id=\"$formId\" />";
  		} else {
  			echo "<fb:dialog-button type=\"button\" value=\"$possitive\" form_id=\"$formId\" href=\"$aprovePage\"  />";
		}
		echo "</fb:dialog>";
  	}


	/*
	* @ params: 	$dialogId - string - the name of the dialog. Must be unique.
	*				$source - string - the page in which the time settings dialog will appear (example: "sendMessage").
	* @ purpose:	function creates the time settings dialog that will open if the user chooses
	*				to set his/her timezone.
	* @ NOTES:		after this function there should be a line (or more) like this:
	*				"<a href=\"#\" clicktoshowdialog=\"$dialogId\"><font size=\"2\">time zone</font></a>"
	*				to call the dialog box.
	* @ return:		void.
	*/
	function openTimeSettingsDialog($dialogId,$source)
	{
		$dialogTitle = "Set Your Time Zone";
		$formId = "set_time";
		$dialogContent = "
				<form id=\"".$formId."\" action=\"$source.php\" method=\"post\"> 
				Select the time zone in which you wish to see the messages:
				<select name=\"timeZoneSelect\">
					<option value=\"-12\">-12</option>
					<option value=\"-11\">-11</option>
					<option value=\"-10\">-10</option>
					<option value=\"-9.5\">-9.5</option>
					<option value=\"-9\">-09</option>
					<option value=\"-8.5\">-8.5</option>
					<option value=\"-8\">-08 PST</option>
					<option value=\"-7\">-07 MST</option>
					<option value=\"-6\">-06 CST</option>
					<option value=\"-5\">-05 EST</option>
					<option value=\"-4\">-04 AST</option>
					<option value=\"-3.5\">-3.5</option>
					<option value=\"-3\">-03 ADT</option>
					<option value=\"-2\">-02</option>
					<option value=\"-1\">-01</option>
					<option value=\"0\" selected=\"selected\">00 GMT</option>
					<option value=\"1\">+01 CET</option>
					<option value=\"2\">+02</option>
					<option value=\"3\">+03</option>
					<option value=\"3.5\">+03.5</option>
					<option value=\"4\">+04</option>
					<option value=\"4.5\">+04.5</option>
					<option value=\"5\">+05</option>
					<option value=\"5.5\">+05.5</option>
					<option value=\"6\">+06</option>
					<option value=\"6.5\">+06.5</option>
					<option value=\"7\">+07</option>
					<option value=\"8\">+08</option>
					<option value=\"9\">+09</option>
					<option value=\"9.5\">+09.5</option>
					<option value=\"10\">+10</option>
					<option value=\"10.5\">+10.5</option>
					<option value=\"11\">+11</option>
					<option value=\"11.5\">+11.5</option>
					<option value=\"12\">+12</option>
					<option value=\"13\">+13</option>
					<option value=\"14\">+14</option> 
				</select>
				</form></br>";
		createFaceBookDialog($dialogId,$dialogTitle,$dialogContent,$formId,NULL,"Submit Change");
	}	
	
	/*
	* @ params: 	$dialogId - string - the name of the dialog. Must be unique.
	*				$source - string - the page in which the delete dialog will appear (example: "sendMessage").
	*				$message_name - string - the name of the message to be deleted. default: 'this message'.
	*				$filename - string - the name of the file of the specified message.
	*				$index - int - the index of the specified message in the DB table (messages_data).
	*				$recipient - 64bit int - if this is a response process - the user id of the one we want
	*							to respond to. Otherwise - NULL.
	* @ purpose:	function creates the delete dialog that will appear when user will choose to delete.
	* @ NOTES:		after this function there should be a line (or more) like this:
	*				"<a href=\"#\" clicktoshowdialog=\"$dialogId\"><font size=\"2\">delete</font></a>"
	*				to call the dialog box.
	* @ return:		void.
	*/
	function createDeleteDialog($dialogId,$source,$message_name='this message',$filename,$index,$recipient)
	{
		$dialogTitle = "Are you sure?";
		$formId = "delete_form_".$index;
		$dialogContent = "Are you sure you want to delete <b>$message_name</b>?
							<form id=\"".$formId."\" action=\"$source.php\" method=\"post\"> 
							<input type=\"hidden\" name=\"delete\" value=\"delete\">
							<input type=\"hidden\" name=\"index\" value=\"$index\">
							";
		if ($filename!=NULL) {
			$dialogContent = $dialogContent."<input type=\"hidden\" name=\"filename\" value=\"$filename\">";
		} else {
			$dialogContent = $dialogContent."<input type=\"hidden\" name=\"recipient\" value=\"$recipient\">";
		}
		$dialogContent = $dialogContent."</form></br>";
		createFaceBookDialog($dialogId,$dialogTitle,$dialogContent,$formId,NULL,"Yes");
	}

	/*
	* @ purpose:	function prints the call for the play applet (using iframe).
	* @ return:		void.
	*/
	function activatePlayApplet($server,$filename)
	{
		echo "
		<fb:swf 
		swfbgcolor='#d8dfea' 
		swfsrc='$server/Player.swf' 
		width='100' 
		height='100' 
		flashvars='&filename=$filename'
		waitforclick='false'
		loop='false'
		/>
		";
	}

	/*
	* @ params: 	$ids - a comma separated list of user ids that sending failed for them.
	* @ purpose:	function turns this list to a list of the users names.
	* @ return:		the string that when echo()ed will result in the list of names (facebook format).
	*/
	function getNamesList($ids)
	{
		$names_list = "";
		$token = strtok($ids, ",");
		while ($token !== false)
		{
		  if ($names_list=="") {
		  	$names_list = "<fb:name uid=\"".$token."\" />";
		  } else {
		  	$names_list = $names_list.",<fb:name uid=\"".$token."\" />";
		  }
		  $token = strtok(" ");
		}
		return $names_list;
	}
	

	/*
	* @ params: 	$index - the index of the relevant message
	*				$recorder_id - the user id of the message sender.
	* @ purpose:	function creates the form that is submitted in order to 
	*				respond to a specific message.
	* @ return:		void.
	*/
function createRespondForm($index,$recorder_id){
	echo "<form id=\"respond_$index\" action=\"sendMessage.php\" method=\"post\">
			
			<input type=\"hidden\" name=\"recipient\" value=\"$recorder_id\"/>
	     </form>";
	//<input type=\"hidden\" name=\"respond\" value=\"respond\"/>
}

	/*
	* @ params: 	$index - the index of the relevant message
	*				$recipient - the user id of the recipient, if the user came from
	*					a respond command. 0 otherwise.
	* @ purpose:	function creates the form that is submitted in order to 
	*				send a specific message.
	* @ return:		void.
	*/
function createSendForm($index,$recipient){
    echo "<form id=\"send_$index\" action=\"send.php\" method=\"post\">
			<input type=\"hidden\" name=\"newMsg\" value=\"0\"/>
			<input type=\"hidden\" name=\"index\" value=\"$index\"/>
			<input type=\"hidden\" name=\"recipient\" value=\"$recipient\"/>
          </form>";
    //<input type=\"hidden\" name=\"send\" value=\"send\"/>
}

	/*
	* @ params: 	$type - an integer that stands for the table that is presented:
	*					1 - recieved messages, 2 - recorded messages, 3 - pre recorded messages.
	*				$messageName - the name of the message that this will be it's dialog.
	*				$filename - relevant filename of this message.
	*				$index - relevant index of this message.
	*				$is_read - if this message was heard before: 1, otherwise: 0.
	*				$recorder_id - the user ID of the recorder of this message.
	*				$recipient - if the user got here via respond command, this is the recipient ID.
	*				$sourceURL - the URL the user came from.
	*				$server - the server.
	* @ purpose:	function builds the dialog that will open if the user clicks on a message
	*				in the table. Dialog will allow user to play message, delete message, respond to message
	*				and send message (as relevant to the table type).
	* @ return:		void.
	*/
function createOptionsDialog($type,$messageName,$filename,$index,$is_read,$recorder_id,$recipient,$sourceURL)
{
	echo "<fb:dialog id=\"options_$index\" >
			<fb:dialog-title>".
			"Message \"".$messageName."\"".
			"</fb:dialog-title>	
			<fb:dialog-content>
			<div id=\"main_options_$index\">
			What would you like to do?";
	if($type==1){
		createRespondForm($index,$recorder_id);
	} else {
		createSendForm($index,$recipient);
	}
	echo "</div>
			</fb:dialog-content>
			<fb:dialog-button id=\"play_button\" type=\"button\" value=\"Play\" onclick='showPlayDialog(\"$filename\",\"$messageName\",\"$index\",$is_read,\"$sourceURL\"); Animation(document.getElementById(\"options_$index\").getParentNode().getParentNode().getParentNode()).hide().go();'/>";
	if($type!=3){
		echo "<fb:dialog-button id=\"delete_button\" type=\"button\" value=\"Delete\" onclick='Animation(document.getElementById(\"options_$index\").getParentNode().getParentNode().getParentNode()).hide().go();' clicktoshowdialog=\"delete_dialog_$index\" />"; 
	}
	if($type!=1){
		echo "<fb:dialog-button type=\"submit\" value=\"Send\" form_id=\"send_$index\" />";
	}
	if($type==1){
		echo "<fb:dialog-button type=\"submit\" value=\"Respond to sender\" form_id=\"respond_$index\" />";
	}
	echo "<fb:dialog-button type=\"submit\" value=\"Cancel\" close_dialog=1 />";
	echo "</fb:dialog>";
}
	
	/*
	* @ params: 	$sqlDb - mysqli database object
	*				$user_id - the id of the user whos messages we need to
	*				present on page.
	*				$timeOffset - the user's time offset (from GMT).
	* @ purpose:	function presents the table of messages the user got from
	*				others. Data displayed: sender id, name of message, time it was
	*				entered to DB.
	* @ return:		TRUE for success, FALSE for failure..
	*/
	function presentReceivedMessages($sqlDb,$user_id,$timeOffset)
	{
		if (($messagesArr = createMessagesArray($sqlDb,$user_id,"received"))==-1) {
			return FALSE;
		}
		if (($arrSize = count($messagesArr))==0) {
			echo "<h2 class=\"noMessages\">You have no messages at the moment.</h2>";
			return TRUE;
		} else {
			// set sizes according to browser:
			$cellBasicHeight = 57;
			//set up the table: 
			   echo "<table class=\"tableone\" align=\"left\">
      					<thead>
        					<tr>
          						<th class=\"th1\" scope=\"col\">From</th>
          						<th class=\"th2\" scope=\"col\"></th>
          						<th class=\"th3\" scope=\"col\">Message</th>
          						<th class=\"th4\" scope=\"col\">Sent At</th>
          					</tr>
	          			</thead>
						<tfoot>
							<tr>
								<td colspan=\"4\" style=\"padding-left:2px\">Total: $arrSize messages</td>
							</tr>
						</tfoot>
						<tbody>
						<tr>
							<td colspan=\"4\">";
			   if($arrSize<6) {
		    	$height = $arrSize * $cellBasicHeight;
		    	echo "<div class=\"innerb\" style=\"height:$height"."px;\">";
		    } else {
				echo "<div class=\"innerb\">";
		    }
		    echo "<table class=\"tabletwo\" >";
			for ($i=0;$i<$arrSize;$i++) {
				// get data of message from array:
				$index = $messagesArr[$i]['index'];
				$recorder_id = $messagesArr[$i]['recorder_id'];
				$message_name = $messagesArr[$i]['message_name'];
				$GMTtime = $messagesArr[$i]['GMTtime'];
				$is_read = $messagesArr[$i]['is_read'];
				$filename = $messagesArr[$i]['filename'];
				//
				if ($is_read==1) {
					echo "<tr clicktoshowdialog=\"options_$index\" onmouseover=\"highlightRow(this);return false\" onmouseout=\"cancelRow(this)\" style=\"cursor: pointer;\">";
				} else {
					echo "<tr class=\"notRead\" clicktoshowdialog=\"options_$index\" onmouseover=\"highlightRow(this);return false\" onmouseout=\"cancelRow(this)\" style=\"cursor: pointer;\">";
				}
				echo "<td class=\"td1\">";
				$dialogId = "delete_dialog_$index";
				createDeleteDialog($dialogId,'getMessages',$message_name,$filename,$index,0);
				createOptionsDialog(1,$message_name,$filename,$index,$is_read,$recorder_id,NULL,"getMessages.php");
				echo "<div style=\"margin-top:2px\">
					<fb:profile-pic uid=\"$recorder_id\" linked=\"false\" size=\"square\"/>
					</div>
					</td>
					<td class=\"td2\">
					<font valign=\"center\"><fb:name uid=\"$recorder_id\"/></font>
					
					</td>";
				echo "<td class=\"td3\">";
				echo "$message_name
						</td>";
				echo "<td class=\"td4\">";
				$userTime = makeUserTimeFromGMT($sqlDb,$GMTtime,$timeOffset);
				echo "$userTime
						</td>
						</tr>";
			}
			echo "</table>
					</div>
					</td></tr>
					</tbody>
					</table>";
			return TRUE;
		}
	}	


	/*
	* @ params: 	$sqlDb - mysqli database object
	*				$user_id - the id of the user whos messages we need to
	*					present on page.
	*				$timeOffset - the user's time offset (from GMT).
	*				$recipient - if the user got here via respond command, this is the
	*					recipient'a user ID.
	* @ purpose:	function presents the table of messages the user recorded.
	*				Data displayed: name of message, time it was
	*				entered to DB.
	* @ return:		TRUE for success, FALSE for failure.
	*/

	function presentRecordedMessages($sqlDb,$user_id,$timeOffset,$recipient=0)
	{
		
    	if (($messagesArr = createMessagesArray($sqlDb,$user_id,"recorded"))==-1) {
    		return FALSE;
    	}
    	if (($arrSize=count($messagesArr))==0) {
    		echo "<h2 class=\"noMessages\">You have no recorded messages at the moment.</h2>";
			return TRUE;
    	} else {
			// set sizes according to browser:
    		if($browser = get_browser(null, true)) {
    			if (!(strcmp($browser['browser'],"IE"))) {
    				//set sizes to IE
    				$cellHeightUnder5 = 18;
    				$cellBasicHeight = 20;
    			} else {
	    			//set to FireFox
	    			$cellHeightUnder5 = 15;
	    			$cellBasicHeight = 18;
    			} 
			} else {
				$cellHeightUnder5 = 18;
				$cellBasicHeight = 15;
			}
			//set up the table: 
		    echo "<table class=\"tableone\" align=\"left\">
      					<thead>
        					<tr>
          						<th class=\"th1\" style=\"width:300px;\" scope=\"col\">Message</th>
          						<th class=\"th2\" style=\"width:300px;\" scope=\"col\">Recorded At</th>
          					</tr>
	          			</thead>
						<tfoot>
							<tr>
								<td colspan=\"2\" style=\"padding-left:2px\">Total: $arrSize/10 messages</td>
							</tr>
						</tfoot>
						<tbody>
						<tr>
							<td colspan=\"2\">";
		    if($arrSize<6) {
		    	$height = $arrSize * $cellBasicHeight;
		    	echo "<div class=\"innerb\" style=\"height:$height"."px;\">";
		    } else {
				echo "<div class=\"innerb\" style=\"height:".(5 * $cellBasicHeight)."px;\">";
		    }
			echo "<table class=\"tabletwo\">"; 
			for ($i=0;$i<$arrSize;$i++) {
				// get data of message from array:
				$index = $messagesArr[$i]['index'];
				$message_name = $messagesArr[$i]['message_name'];
				$filename = $messagesArr[$i]['filename'];
				$GMTtime = $messagesArr[$i]['GMTtime'];
				// put data in table:
				echo "<tr clicktoshowdialog=\"options_$index\"  onmouseover=\"highlightRow(this);return false\" onmouseout=\"cancelRow(this)\" style=\"cursor: pointer;\">";
				echo "<td class=\"td1\" style=\"width:300px;height:".$cellHeightUnder5."px;\">";
				$dialogId = "delete_dialog_".$index;
				createDeleteDialog($dialogId,'sendMessage',$message_name,$filename,$index,$recipient);
				createOptionsDialog(2,$message_name,$filename,$index,1,NULL,$recipient,"sendMessage.php");
				echo "$message_name
					</td>";
				$userTime = makeUserTimeFromGMT($sqlDb,$GMTtime,$timeOffset);
				echo "<td class=\"td2\" style=\"width:280px; height:".$cellHeightUnder5."px;\">
						$userTime
					</td>
					</tr>";
			}
			echo "</table>
					</div>
					</td></tr>
					</tbody>
					</table>";
			return TRUE;
    	}
	}
	
	/*
	* @ params: 	$sqlDb - mysqli database object
	*				$user_id - the id of the user whos messages we need to
	*				present on page.
	* @ purpose:	function presents the table of messages the user recorded.
	*				Data displayed: name of message, time it was
	*				entered to DB.
	* @ return:		FALSE if an error occured, TRUE otherwise.
	*/
	function presentPreRecordedMessages($sqlDb,$recipient=0)
	{
		if (($messagesArr = createMessagesArray($sqlDb,$user_id,"preRecorded"))==-1) {
    		return FALSE;
    	}
    	if (($arrSize=count($messagesArr))==0) {
    		echo "<h2 class=\"noMessages\">There are no pre-recorded messages available at the moment.</h2>";
			return TRUE;
    	} else {
   			// set sizes according to browser:
    		if($browser = get_browser(null, true)) {
    			if (!(strcmp($browser['browser'],"IE"))) {
    				//set sizes to IE
    				$cellHeightUnder5 = 18;
    				$cellBasicHeight = 20;
    			} else {
	    			//set to FireFox
	    			$cellHeightUnder5 = 15;
	    			$cellBasicHeight = 18;
    			} 
			} else {
				$cellHeightUnder5 = 15;
	    		$cellBasicHeight = 18;
			}
			//set up table:
			echo "<table class=\"tableone\" align=\"left\" style=\"width:350px\">
      					<thead>
        					<tr>
          						<th class=\"th1\" style=\"width:350px;\" scope=\"col\">Message</th>
          					</tr>
	          			</thead>
						<tfoot>
							<tr>
								<td colspan=\"2\" style=\"padding-left:2px\">Total: $arrSize messages</td>
							</tr>
						</tfoot>
						<tbody>
						<tr>
							<td colspan=\"2\">";
			if($arrSize<6) {
		    	$height = $arrSize * $cellBasicHeight;
		    	echo "<div class=\"innerb\" style=\"height:$height"."px;\">";
		    } else {
				echo "<div class=\"innerb\" style=\"height:".(5*$cellBasicHeight)."px\">";
		    }
			echo "<table class=\"tabletwo\" style=\"width:330px;\">";    		
			for ($i=0;$i<$arrSize;$i++) {
				// get data of message from array:
				$index = $messagesArr[$i]['index'];
				$message_name = $messagesArr[$i]['message_name'];
				$filename = $messagesArr[$i]['filename'];
				// put data in table:
				echo "<tr clicktoshowdialog=\"options_$index\" onmouseover=\"highlightRow(this);return false\" onmouseout=\"cancelRow(this)\" style=\"cursor: pointer;\">";
                echo "<td class=\"td1\" style=\"width:330px; height:".$cellHeightUnder5."px;\">";
                createOptionsDialog(3,$message_name,$filename,$index,1,NULL,NULL,"sendMessage.php");
				echo"$message_name
					</td>
					</tr>";
			}
			echo "</table>
					</div>
					</td></tr>
					</tbody>
					</table>";						
			return TRUE;
			}    		
	}

	/*
	* @ params: 	$recipientsWithout - a list of the user ids of friends without the application.
	*				$user_id - the user id of the current user (sender).
	*				$facebook - the facebook object of this user.
	*				$failList - a list of the user ids that the message failed to 
	*							be sent to. ==> input was canceled.
	* @ purpose:	function opens a multi-friends selector to invite the friends
	*				without the app. Function sends the invitations (requests) and
	*				redirects user to the appropriate page.
	* @ return:		TRUE for success, FALSE for failure.
	*/	
	function inviteRecipients($recipientsWithout,$user_id,$facebook)
	{
		$exclude = "";
		// get list of friends
		try {
			$result = $facebook->api_client->fql_query("SELECT uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = $user_id)");
		} catch (Exception $ex) {
			echo $ex;
		}
		if ($result) {
			for ( $i = 0; $i < count($result); $i++ ) {
				$friend = $result[$i]["uid"];
				if (stristr($recipientsWithout,$friend)==FALSE) {
					if ($exclude=="") {
						$exclude = $friend;
					} else {
						$exclude = $exclude.",".$friend;
					}
				}
			}
			echo "<fb:request-form ";
/*			if ($failList != "") {
				echo "action=\"sendMessage.php?success=$failList\"";
			} else {
*/
				echo "action=\"sendMessage.php?success=all\"";
//			}
			echo "	max=\"10\"
					method=\"POST\" 
					invite=\"false\" 
					type=\"Voice Messenger!\" 
					content=\"I thought about you and sent you a voice message. You should check it out!.";
			echo htmlentities("<fb:req-choice url=\" http://apps.facebook.com/voicemessenger/index.php\" label=\"Hear it now!\" />");
			echo "\">";
			echo "<fb:multi-friend-selector 
					showborder=\"false\" 
					exclude_ids=$exclude
					actiontext=\"These friends do not have VoiceMessenger, yet. Please invite them!
					(Your message will be waiting for them when they add VoiceMessenger)\">
					<fb:request-form-submit/>";
			echo "</fb:request-form>";
			return TRUE;
		}
		return FALSE;
	}
	
	/*
	* @ params: 	$user_id - the user id of the user whos profile will be changes.
	*				$numberOfNewMessages - the number of new messages the user has.
	*				$numberOfSentMessages - the number of messages sent by the user so far.
	*				$facebook - the facebook object of this user.
	* @ purpose:	function updated user's profile box.
	* @ return:		1 for success, 0 for failure.
	*/	
	function profileBox($user_id,$numberOfNewMessages,$numberOfSentMessages,$facebook)
	{
		if($numberOfNewMessages == 0){	
		$fbml = <<<EndHereDoc
				<font size="2" color="red"><center>There are no new messages waiting for <fb:name uid="profileowner" useyou="true" firstnameonly ="true" capitalize="true" /></center></font>
					<fb:visible-to-owner>
						<fb:subtitle> 
						</fb:if-user-has-added-app>
							<a href="http://apps.facebook.com/voicemessenger/index.php?api_key=63b339a2c2ceb8b6728794a5501ea627">Home</a>
							<fb:action href="http://apps.facebook.com/voicemessenger/help.php?api_key=63b339a2c2ceb8b6728794a5501ea627">Help Me!!</fb:action>
						</fb:subtitle>
					</fb:visible-to-owner>
				<div align="center">
					<img src="http://64.22.126.39/voiceMessenger_envalope.JPG" width="80" height="50" />
				</div>
				<b><font size="2" color="black"><center><fb:name uid="profileowner" useyou="true" firstnameonly ="true" capitalize="true" /> sent <font size="3" color="#990000"> $numberOfSentMessages messages</font> to friends</center></font></b>
				</br>
				<a href="http://apps.facebook.com/voicemessenger/sendMessage.php?api_key=63b339a2c2ceb8b6728794a5501ea627">
				<b><font size="3" color="#00008B"><center>Have you sent a message to someone today?</center></font></b></a>
EndHereDoc;
		} elseif($numberOfNewMessages == 1){
			$fbml = <<<EndHereDoc
			<font size="2" color="red"><center>There is a new message waiting for <fb:name uid="profileowner" useyou="true" firstnameonly ="true" capitalize="true" /></center></font>
			<fb:visible-to-owner>
				<fb:subtitle> 
				</fb:if-user-has-added-app>
					<a href="http://apps.facebook.com/voicemessenger/index.php?api_key=63b339a2c2ceb8b6728794a5501ea627">Home</a>
					<fb:action href="http://apps.facebook.com/voicemessenger/help.php?api_key=63b339a2c2ceb8b6728794a5501ea627">Help Me!!</fb:action>
	            </fb:subtitle>
				<a href="http://apps.facebook.com/voicemessenger/getMessages.php?api_key=63b339a2c2ceb8b6728794a5501ea627">
				<font size="2"><center>I want to listen to it!!!</center></font></a>
			</fb:visible-to-owner>
			<div align="center">
			<img src="http://64.22.126.39/voiceMessenger_envalope.JPG" width="80" height="50" />
			</div>
			<b><font size="2" color="black"><center><fb:name uid="profileowner" useyou="true" firstnameonly ="true" capitalize="true" /> sent <font size="3" color="#990000"> $numberOfSentMessages messages</font> to friends</center></font></b>
			</br>
			<a href="http://apps.facebook.com/voicemessenger/sendMessage.php?api_key=63b339a2c2ceb8b6728794a5501ea627">
				<b><font size="3" color="#00008B"><center>Have you sent a message to someone today?</center></font></b></a>
EndHereDoc;
		} else{
			$fbml = <<<EndHereDoc
			<font size="2" color="red"><center>There are $numberOfNewMessages new messages waiting for <fb:name uid="profileowner" useyou="true" firstnameonly ="true" capitalize="true" /></center></font>
			<fb:visible-to-owner>
				<fb:subtitle> 
				</fb:if-user-has-added-app>
					<a href="http://apps.facebook.com/voicemessenger/index.php?api_key=63b339a2c2ceb8b6728794a5501ea627">Home</a>
					<fb:action href="http://apps.facebook.com/voicemessenger/help.php?api_key=63b339a2c2ceb8b6728794a5501ea627">Help Me!!</fb:action>
	            </fb:subtitle>
				<a href="http://apps.facebook.com/voicemessenger/getMessages.php?api_key=63b339a2c2ceb8b6728794a5501ea627">
				<font size="2"><center>I want to listen to them!!!</center></font></a>
			</fb:visible-to-owner>
			<div align="center">
			<img src="http://64.22.126.39/voiceMessenger_envalope.JPG" width="80" height="50" />
			</div>
			<b><font size="2" color="black"><center><fb:name uid="profileowner" useyou="true" firstnameonly ="true" capitalize="true" />
				  sent <font size="3" color="#990000"> $numberOfSentMessages messages</font> to friends</center></font></b>
			</br>
			<a href="http://apps.facebook.com/voicemessenger/sendMessage.php?api_key=63b339a2c2ceb8b6728794a5501ea627">
				<b><font size="3" color="#00008B"><center>Have you sent a message to someone today?</center></font></b></a>
EndHereDoc;
		}
		$profile_action = '<fb:profile-action url="http://apps.facebook.com/voicemessenger/sendMessage.php?api_key=63b339a2c2ceb8b6728794a5501ea627">Send a voice message</fb:profile-action>';	
		
		try {
			$facebook->api_client->profile_setFBML('',$user_id,$fbml, $profile_action,'');
		} catch (Exception $ex) {
			return 0;
		}
		return 1;
	}
?>
